home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Camelot / Camelot 030 (1988-11)(Swedish User Group of Amiga)(SE)(PD)[WB].zip / Camelot 030 (1988-11)(Swedish User Group of Amiga)(SE)(PD)[WB].adf / IFFDevCon / anim / anim.spec next >
Text File  |  1988-05-04  |  24KB  |  460 lines

  1.                               A N I M
  2.                   An IFF Format For CEL Animations
  3.  
  4.                     Revision date:  17 March 1988
  5.  
  6.                      prepared by:
  7.                           SPARTA Inc.
  8.                           23041 de la Carlota
  9.                           Laguna Hills, Calif 92653
  10.                           714) 768-8161
  11.                           contact: Gary Bonham
  12.  
  13.                      also by:
  14.                           Aegis Development Co.
  15.                           2115 Pico Blvd.
  16.                           Santa Monica, Calif 90405
  17.                           213) 392-9972
  18.  
  19.  
  20. 1.0 Introduction
  21.    
  22.    The ANIM IFF format was developed at Sparta originally for the
  23.    production of animated video sequences on the Amiga computer.  The
  24.    intent was to be able to store, and play back, sequences of frames
  25.    and to minimize both the storage space on disk (through compression)
  26.    and playback time (through efficient de-compression algorithms).
  27.    It was desired to maintain maximum compatibility with existing
  28.    IFF formats and to be able to display the initial frame as a normal
  29.    still IFF picture.
  30.    
  31.    Several compression schemes have been introduced in the ANIM format.
  32.    Most of these are strictly of historical interest as the only one
  33.    currently being placed in new code is the vertical run length
  34.    encoded byte encoding developed by Jim Kent.
  35.    
  36.    1.1 ANIM Format Overview
  37.       
  38.       The general philosophy of ANIMs is to present the initial frame
  39.       as a normal, run-length-encoded, IFF picture.  Subsequent
  40.       frames are then described by listing only their differences
  41.       from a previous frame.  Normally, the "previous" frame is two
  42.       frames back as that is the frame remaining in the hidden 
  43.       screen buffer when double-buffering is used.  To better
  44.       understand this, suppose one has two screens, called A and B,
  45.       and the ability to instantly switch the display from one to
  46.       the other.  The normal playback mode is to load the initial
  47.       frame into A and duplicate it into B.  Then frame A is displayed
  48.       on the screen.  Then the differences for frame 2 are used to
  49.       alter screen B and it is displayed.  Then the differences for
  50.       frame 3 are used to alter screen A and it is displayed, and so
  51.       on.  Note that frame 2 is stored as differences from frame 1,
  52.       but all other frames are stored as differences from two frames
  53.       back.
  54.       
  55.       ANIM is an IFF FORM and its basic format is as follows (this
  56.       assumes the reader has a basic understanding of IFF format
  57.       files):
  58.                       FORM ANIM
  59.                       . FORM ILBM         first frame
  60.                       . . BMHD                normal type IFF data
  61.                       . . ANHD                optional animation header
  62.                                               chunk for timing of 1st frame.
  63.                       . . CMAP
  64.                       . . BODY
  65.                       . FORM ILBM         frame 2
  66.                       . . ANHD                animation header chunk
  67.                       . . DLTA                delta mode data
  68.                       . FORM ILBM         frame 3
  69.                       . . ANHD
  70.                       . . DLTA
  71.                            ...
  72.       
  73.       The initial FORM ILBM can contain all the normal ILBM chunks,
  74.       such as CRNG, etc.  The BODY will normally be a standard
  75.       run-length-encoded data chunk (but may be any other legal
  76.       compression mode as indicated by the BMHD).  If desired, an ANHD
  77.       chunk can appear here to provide timing data for the first
  78.       frame.  If it is here, the operation field should be =0.
  79.       
  80.       The subsequent FORMs ILBM contain an ANHD, instead of a BMHD,
  81.       which duplicates some of BMHD and has additional parameters
  82.       pertaining to the animation frame.  The DLTA chunk contains
  83.       the data for the delta compression modes.  If
  84.       the older XOR compression mode is used, then a BODY chunk
  85.       will be here.  In addition, other chunks may be placed in each
  86.       of these as deemed necessary (and as code is placed in player
  87.       programs to utilize them).  A good example would be CMAP chunks
  88.       to alter the color palette.  A basic assumption in ANIMs is
  89.       that the size of the bitmap, and the display mode (e.g. HAM)
  90.       will not change through the animation.  Take care when playing
  91.       an ANIM that if a CMAP occurs with a frame, then the change must
  92.       be applied to both buffers.
  93.       
  94.       Note that the DLTA chunks are not interleaved bitmap representations,
  95.       thus the use of the ILBM form is inappropriate for these frames.  
  96.       However, this inconsistency was not noted until there were a number
  97.       of commercial products either released or close to release which
  98.       generated/played this format.  Therefore, this is probably an
  99.       inconsistency which will have to stay with us.
  100.  
  101.    1.2 Recording ANIMs
  102.  
  103.       To record an ANIM will require three bitmaps - one for 
  104.       creation of the next frame, and two more for a "history" of the
  105.       previous two frames for performing the compression calculations
  106.       (e.g. the delta mode calculations).
  107.       
  108.       There are five frame-to-frame compression methods currently
  109.       defined.  The first three are mainly for historical interest.
  110.       The product Aegis VideoScape 3D utilizes the third method in
  111.       version 1.0, but switched to method 5 on 1.1.  This is
  112.       the only instance known of a commercial product generating
  113.       ANIMs of any of the first three methods.  The fourth method
  114.       is a general short or long word compression scheme which has
  115.       several options including whether the compression is horizontal
  116.       or vertical, and whether or not it is XOR format.  This offers
  117.       a choice to the user for the optimization of file size and/or
  118.       playback speed.  The fifth method is the byte vertical run length
  119.       encoding as designed by Jim Kent.  Do not confuse
  120.       this with Jim's RIFF file format which is different than ANIM.
  121.       Here we utilized his compression/decompression routines within the
  122.       ANIM file structure.
  123.       
  124.       The following paragraphs give a general outline of each of the
  125.       methods of compression currently included in this spec.
  126.  
  127.       1.2.1 XOR mode
  128.          
  129.          This mode is the original and is included here for historical
  130.          interest.  In general, the delta modes are far superior.
  131.          The creation of XOR mode is quite simple.  One simply
  132.          performs an exclusive-or (XOR) between all corresponding
  133.          bytes of the new frame and two frames back.  This results
  134.          in a new bitmap with 0 bits wherever the two frames were
  135.          identical, and 1 bits where they are different.  Then this
  136.          new bitmap is saved using run-length-encoding.  A major
  137.          obstacle of this mode is in the time consumed in performing
  138.          the XOR upon reconstructing the image.
  139.          
  140.       1.2.2 Long Delta mode
  141.          
  142.          This mode stores the actual new frame long-words which are
  143.          different, along with the offset in the bitmap.  The
  144.          exact format is shown and discussed in section 2 below.
  145.          Each plane is handled separately, with no data being saved
  146.          if no changes take place in a given plane.  Strings of
  147.          2 or more long-words in a row which change can be run
  148.          together so offsets do not have to be saved for each one.
  149.          
  150.          Constructing this data chunk usually consists of having
  151.          a buffer to hold the data, and calculating the data as
  152.          one compares the new frame, long-word by long-word, with
  153.          two frames back.
  154.          
  155.       1.2.3 Short Delta mode
  156.          
  157.          This mode is identical to the Long Delta mode except that
  158.          short-words are saved instead of long-words.  In most
  159.          instances, this mode results in a smaller DLTA chunk.
  160.          The Long Delta mode is mainly of interest in improving
  161.          the playback speed when used on a 32-bit 68020 Turbo Amiga.
  162.          
  163.       1.2.4 General Delta mode
  164.  
  165.          The above two delta compression modes were hastily put together.
  166.          This mode was an attempt to provide a well-thought-out delta
  167.          compression scheme.  Options provide for both short and long
  168.          word compression, either vertical or horizontal compression,
  169.          XOR mode (which permits reverse playback), etc.  About the time
  170.          this was being finalized, the fifth mode, below, was developed
  171.          by Jim Kent.  In practice the short-vertical-run-length-encoded
  172.          deltas in this mode play back faster than the fifth mode (which
  173.          is in essence a byte-vertical-run-length-encoded delta mode) but
  174.          does not compress as well - especially for very noisy data such
  175.          as digitized images.  In most cases, playback speed not being
  176.          terrifically slower, the better compression (sometimes 2x) is
  177.          preferable due to limited storage media in most machines.
  178.  
  179.          Details on this method are contained in section 2.2.2 below.
  180.  
  181.       1.2.5 Byte Vertical Compression
  182.  
  183.          This method does not offer the many options that method 4 offers,
  184.          but is very successful at producing decent compression even for
  185.          very noisy data such as digitized images.  The method was devised
  186.          by Jim Kent and is utilized in his RIFF file format which is 
  187.          different than the ANIM format.  The description of this method
  188.          in this document is taken from Jim's writings.  Further, he has
  189.          released both compression and decompression code to public domain.
  190.          
  191.          Details on this method are contained in section 2.2.3 below.
  192.  
  193.    1.3 Playing ANIMs
  194.       
  195.       Playback of ANIMs will usually require two buffers, as mentioned
  196.       above, and double-buffering between them.  The frame data from
  197.       the ANIM file is used to modify the hidden frame to the next
  198.       frame to be shown.  When using the XOR mode, the usual run-
  199.       length-decoding routine can be easily modified to do the 
  200.       exclusive-or operation required.  Note that runs of zero bytes,
  201.       which will be very common, can be ignored, as an exclusive or
  202.       of any byte value to a byte of zero will not alter the original
  203.       byte value.
  204.       
  205.       The general procedure, for all compression techniques, is to first
  206.       decode the initial ILBM picture into the hidden buffer and double-
  207.       buffer it into view.  Then this picture is copied to the other (now
  208.       hidden) buffer.  At this point each frame is displayed with the
  209.       same procedure.  The next frame is formed in the hidden buffer by
  210.       applying the DLTA data (or the XOR data from the BODY chunk in the
  211.       case of the first XOR method) and the new frame is double-buffered
  212.       into view.  This process continues to the end of the file.
  213.  
  214.       A master colormap should be kept for the entire ANIM which would
  215.       be initially set from the CMAP chunk in the initial ILBM.  This
  216.       colormap should be used for each frame.  If a CMAP chunk appears
  217.       in one of the frames, then this master colormap is updated and the
  218.       new colormap applies to all frames until the occurrance of another
  219.       CMAP chunk.
  220.  
  221.       Looping ANIMs may be constructed by simply making the last two frames
  222.       identical to the first two.  Since the first two frames are special
  223.       cases (the first being a normal ILBM and the second being a delta from
  224.       the first) one can continually loop the anim by repeating from frame
  225.       three.  In this case the delta for creating frame three will modify
  226.       the next to the last frame which is in the hidden buffer (which is
  227.       identical to the first frame), and the delta for creating frame four
  228.       will modify the last frame which is identical to the second frame.
  229.  
  230.       Multi-File ANIMs are also supported so long as the first two frames
  231.       of a subsequent file are identical to the last two frames of the
  232.       preceeding file.  Upon reading subsequent files, the ILBMs for the
  233.       first two frames are simply ignored, and the remaining frames are
  234.       simply appended to the preceeding frames.  This permits splitting
  235.       ANIMs across multiple floppies and also permits playing each section
  236.       independently and/or editing it independent of the rest of the ANIM.
  237.       
  238.       Timing of ANIM playback is easily achieved using the vertical blank
  239.       interrupt of the Amiga.  There is an example of setting up such
  240.       a timer in the ROM Kernel Manual.  Be sure to remember the timer
  241.       value when a frame is flipped up, so the next frame can be flipped
  242.       up relative to that time.  This will make the playback independent
  243.       of how long it takes to decompress a frame (so long as there is enough
  244.       time between frames to accomplish this decompression).
  245.  
  246. 2.0 Chunk Formats
  247.    2.1 ANHD Chunk
  248.       The ANHD chunk consists of the following data structure:
  249.       
  250.            UBYTE operation  The compression method:
  251.                             =0 set directly (normal ILBM BODY),
  252.                             =1 XOR ILBM mode,
  253.                             =2 Long Delta mode,
  254.                             =3 Short Delta mode,
  255.                             =4 Generalized short/long Delta mode,
  256.                             =3 Byte Vertical Delta mode
  257.                             =74 (ascii 'J') reserved for Eric Graham's
  258.                                compression technique (details to be
  259.                                released later).
  260.  
  261.            UBYTE mask      (XOR mode only - plane mask where each
  262.                             bit is set =1 if there is data and =0
  263.                             if not.)
  264.            UWORD w,h       (XOR mode only - width and height of the
  265.                             area represented by the BODY to eliminate
  266.                             unnecessary un-changed data)
  267.            WORD  x,y       (XOR mode only - position of rectangular
  268.                             area representd by the BODY)
  269.            ULONG abstime   (currently unused - timing for a frame
  270.                             relative to the time the first frame
  271.                             was displayed - in jiffies (1/60 sec))
  272.            ULONG reltime   (timing for frame relative to time
  273.                             previous frame was displayed - in
  274.                             jiffies (1/60 sec))
  275.            UBYTE interleave (unused so far - indicates how may frames
  276.                              back this data is to modify.  =0 defaults
  277.                              to indicate two frames back (for double
  278.                              buffering). =n indicates n frames back.
  279.                              The main intent here is to allow values
  280.                              of =1 for special applications where
  281.                              frame data would modify the immediately
  282.                              previous frame)
  283.            UBYTE pad0        Pad byte, not used at present.
  284.            ULONG bits        32 option bits used by options=4 and 5.
  285.                              At present only 6 are identified, but the
  286.                              rest are set =0 so they can be used to
  287.                              implement future ideas.  These are defined
  288.                              for option 4 only at this point.  It is
  289.                              recommended that all bits be set =0 for
  290.                              option 5 and that any bit settings
  291.                              used in the future (such as for XOR mode)
  292.                              be compatible with the option 4
  293.                              bit settings.   Player code should check
  294.                              undefined bits in options 4 and 5 to assure
  295.                              they are zero.
  296.  
  297.                              The six bits for current use are:
  298.  
  299.                              bit #              set =0               set =1
  300.                              ===============================================
  301.                              0              short data           long data
  302.                              1                 set                  XOR
  303.                              2             separate info        one info list
  304.                                            for each plane       for all planes
  305.                              3               not RLC        RLC (run length coded)
  306.                              4              horizontal           vertical
  307.                              5           short info offsets   long info offsets
  308.  
  309.            UBYTE pad[16]     This is a pad for future use for future
  310.                              compression modes.
  311.       
  312.    2.2 DLTA Chunk
  313.       
  314.       This chunk is the basic data chunk used to hold delta compression
  315.       data.  The format of the data will be dependent upon the exact
  316.       compression format selected.  At present there are two basic
  317.       formats for the overall structure of this chunk.
  318.  
  319.       2.2.1 Format for methods 2 & 3
  320.  
  321.          This chunk is a basic data chunk used to hold the delta
  322.          compression data.  The minimum size of this chunk is 32 bytes
  323.          as the first 8 long-words are byte pointers into the chunk for
  324.          the data for each of up to 8 bitplanes.  The pointer for the
  325.          plane data starting immediately following these 8 pointers will
  326.          have a value of 32 as the data starts in the 33-rd byte of the
  327.          chunk (index value of 32 due to zero-base indexing).
  328.       
  329.          The data for a given plane consists of groups of data words.  In
  330.          Long Delta mode, these groups consist of both short and long
  331.          words - short words for offsets and numbers, and long words for
  332.          the actual data.  In Short Delta mode, the groups are identical
  333.          except data words are also shorts so all data is short words.
  334.          Each group consists of a starting word which is an offset.  If
  335.          the offset is positive then it indicates the increment in long
  336.          or short words (whichever is appropriate) through the bitplane.
  337.          In other words, if you were reconstructing the plane, you would
  338.          start a pointer (to shorts or longs depending on the mode) to
  339.          point to the first word of the bitplane.  Then the offset would
  340.          be added to it and the following data word would be placed at
  341.          that position.  Then the next offset would be added to the
  342.          pointer and the following data word would be placed at that
  343.          position.  And so on...  The data terminates with an offset
  344.          equal to 0xFFFF.
  345.       
  346.          A second interpretation is given if the offset is negative.  In
  347.          that case, the absolute value is the offset+2.  Then the 
  348.          following short-word indicates the number of data words that
  349.          follow.  Following that is the indicated number of contiguous
  350.          data words (longs or shorts depending on mode) which are to
  351.          be placed in contiguous locations of the bitplane.
  352.       
  353.          If there are no changed words in a given plane, then the pointer
  354.          in the first 32 bytes of the chunk is =0.
  355.       
  356.       2.2.2 Format for method 4
  357.          
  358.          The DLTA chunk is modified slightly to have 16 long pointers at
  359.          the start.  The first 8 are as before - pointers to the start of
  360.          the data for each of the bitplanes (up to a theoretical max of 8
  361.          planes).  The next 8 are pointers to the start of the offset/numbers
  362.          data list.  If there is only one list of offset/numbers for all
  363.          planes, then the pointer to that list is repeated in all positions
  364.          so the playback code need not even be aware of it.  In fact, one
  365.          could get fancy and have some bitplanes share lists while others
  366.          have different lists, or no lists (the problems in these schemes
  367.          lie in the generation, not in the playback).
  368.  
  369.          The best way to show the use of this format is in a sample playback
  370.          routine.
  371.  
  372.             SetDLTAshort(bm,deltaword)
  373.             struct BitMap *bm;
  374.             WORD *deltaword;
  375.             {
  376.                int i;
  377.                LONG *deltadata;
  378.                WORD *ptr,*planeptr;
  379.                register int s,size,nw;
  380.                register WORD *data,*dest;
  381.  
  382.                deltadata = (LONG *)deltaword;
  383.                nw = bm->BytesPerRow >>1;
  384.  
  385.                for (i=0;i<bm->Depth;i++) {
  386.                   planeptr = (WORD *)(bm->Planes[i]);
  387.                   data = deltaword + deltadata[i];
  388.                   ptr  = deltaword + deltadata[i+8];
  389.                   while (*ptr != 0xFFFF) {
  390.                      dest = planeptr + *ptr++;
  391.                      size = *ptr++;
  392.                      if (size < 0) {
  393.                         for (s=size;s<0;s++) {
  394.                            *dest = *data;
  395.                            dest += nw;
  396.                         }
  397.                         data++;
  398.                      }
  399.                      else {
  400.                         for (s=0;s<size;s++) {
  401.                            *dest = *data++;
  402.                            dest += nw;
  403.                         }
  404.                      }
  405.                   }
  406.                }
  407.                return(0);
  408.             }
  409.  
  410.          The above routine is for short word vertical compression with
  411.          run length compression.  The most efficient way to support 
  412.          the various options is to replicate this routine and make 
  413.          alterations for, say, long word or XOR.  The variable nw
  414.          indicates the number of words to skip to go down the vertical
  415.          column.  This one routine could easily handle horizontal
  416.          compression by simply setting nw=1.  For ultimate playback
  417.          speed, the core, at least, of this routine should be coded in
  418.          assembly language.
  419.  
  420.       2.2.2 Format for method 5
  421.  
  422.          In this method the same 16 pointers are used as in option 4.
  423.          The first 8 are pointers to the data for up to 8 planes.
  424.          The second set of 8 are not used but were retained for several
  425.          reasons.  First to be somewhat compatible with code for option
  426.          4 (although this has not proven to be of any benefit) and 
  427.          second, to allow extending the format for more bitplanes (code
  428.          has been written for up to 12 planes).  
  429.  
  430.          Compression/decompression is performed on a plane-by-plane basis.
  431.          For each plane, compression can be handled by the skip.c code
  432.          (provided Public Domain by Jim Kent) and decompression can be
  433.          handled by unvscomp.asm (also provided Public Domain by Jim Kent).
  434.          
  435.          Compression/decompression is performed on a plane-by-plane basis.
  436.          The following description of the method is taken directly from
  437.          Jim Kent's code with minor re-wording.  Please refer to Jim's
  438.          code (skip.c and unvscomp.asm) for more details:
  439.  
  440.             Each column of the bitplane is compressed separately.
  441.             A 320x200 bitplane would have 40 columns of 200 bytes each.
  442.             Each column starts with an op-count followed by a number
  443.             of ops.  If the op-count is zero, that's ok, it just means
  444.             there's no change in this column from the last frame.
  445.             The ops are of three classes, and followed by a varying
  446.             amount of data depending on which class:
  447.               1. Skip ops - this is a byte with the hi bit clear that
  448.                  says how many rows to move the "dest" pointer forward,
  449.                  ie to skip. It is non-zero.
  450.               2. Uniq ops - this is a byte with the hi bit set.  The hi
  451.                  bit is masked down and the remainder is a count of the
  452.                  number of bytes of data to copy literally.  It's of
  453.                  course followed by the data to copy.
  454.               3. Same ops - this is a 0 byte followed by a count byte,
  455.                  followed by a byte value to repeat count times.
  456.             Do bear in mind that the data is compressed vertically rather
  457.             than horizontally, so to get to the next byte in the destination
  458.             we add the number of bytes per row instead of one!
  459.  
  460.